package com.dingding.gmall.order.vo;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dingding.gmall.order.dto.OrderItemDTO;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author liugongding
 * @description:
 * @date 2022/2/25 9:04 PM
 */

public class OrderTradeVO {

    /**
     * 收货地址
     */
    @Getter @Setter
    private List<MemberAddressVO> memberAddressVOList;

    /**
     * 选中的购物项
     */
    @Getter @Setter
    private List<OrderItemDTO> orderItemDTOList;

    //发票...
    /**
     * 优惠券
     * 积分 - 100积分自动换算成1元
     * 取整数
     */
    @Setter @Getter
    private BigDecimal integralReducePrice;

    public BigDecimal calculateIntegralReducePrice(Integer integration){
        double floor = Math.floor(integration / 100);
        return new BigDecimal(floor);
    }

    /**
     * 订单总额
     */
    @Getter @Setter
    private BigDecimal orderPrice;

    public BigDecimal getOrderPrice() {
        BigDecimal bigDecimal = new BigDecimal("0.00");
        // 计算需要支付的价格
        if (CollectionUtils.isNotEmpty(orderItemDTOList)){
            return orderItemDTOList.stream().map(OrderItemDTO::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        return bigDecimal;
    }

    /**
     * 实际支付的价格
     * 加上运费，优惠券等价格
     */
    @Setter
    private BigDecimal actuallyPaidPrice;

    public BigDecimal getActuallyPaidPrice() {
        return getOrderPrice().subtract(this.integralReducePrice);
    }

    /**
     * 是否有货
     */
    @Getter @Setter
    Map<Long,Boolean> stocks;

    /**
     * 防重复提交唯一令牌
     */
    @Getter @Setter
    private String orderToken;

    @Setter
    private Integer goodsCount;

    public Integer getGoodsCount(){
        Integer goodsCount = 0;
        if (CollectionUtils.isNotEmpty(orderItemDTOList)) {
            goodsCount = orderItemDTOList.stream().mapToInt(OrderItemDTO::getItemCount).sum();
        }
        return goodsCount;
    }


}
